package com.demo.example.hot100;

public class DeleteTheNListNode {
    public static ListNode removeNthFromEnd(ListNode head,int n){
        //删除链表的倒数第N个节点，快慢指针
        if(head==null || (head.next==null && n==1)){
            return null;
        }
        ListNode fast=head;
        ListNode slow=head;
        ListNode cur=head;
        int length=0;
        while(cur!=null){
            cur=cur.next;
            length++;
        }
        if(length==n){
            return head.next;
        }
        for(int i=0;i<n-1;i++){
            fast=fast.next;
        }
        while(fast.next.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        slow.next=slow.next.next;
        return head;
    }
}
